<?php

namespace humhub\modules\api\models;

use Yii;
use humhub\modules\post\models\Post;
use humhub\modules\content\components\ContentAddonActiveRecord;


class TopList extends ContentAddonActiveRecord
{

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'post_top';
    }

   /**
     * area 位置映射关系
     */
    private function gettype($area){
        $x = array(
            2 => array( 'id'=>'2', 'url' => 'indextop',  'name'=>'首页置顶内容'),
            3 => array( 'id'=>'3', 'url' => 'indexlist', 'name'=>'首页内容流内容'),
            4 => array( 'id'=>'4', 'url' => 'findtop',   'name'=>'发现置顶内容'),
            5 => array( 'id'=>'5', 'url' => 'findlist',  'name'=>'发现列表内容'),
        );
        if( isset($x[$area]) ){
            return $x[$area];
        }
        return false;
    }

    /**
     * 获取指定位置按钮列表
     *
     * 直播的 toptype=2 ,需要特殊处理
     */
    public function getList($area=0, $page=1){
        $_t = self::gettype($area);
        if( $_t === false ){
            return Array(
                'list' => [],
            );
        }

        $limit = 5;
        if( $page>1 ){
            $offset = ($page-1) * $limit;
        }else{
            $offset = 0;
        }

        $list2 = (new \yii\db\Query())
            ->select("topid,toptype, title, imgguid, postcount, url ,focus_img ,sguid")
            ->from('post_top')->where('area='.$area.' AND toptype!=2 AND status=1')
            ->offset($offset)->limit($limit)
            ->orderBy('topid DESC')
            ->all();
        foreach($list2 AS $key=>$one){
            $img = '';
            $file = \humhub\modules\file\models\File::findOne(['guid' => $one['imgguid']]);
            if( $file ){
                if ($file->getMimeBaseType() == "image"){
                    //$img = $file->getPreviewImageUrl(200, 200);
                    $img = $file->getUrl();
                }
            }
            unset($list2[$key]['imgguid']);
            $list2[$key]['image'] = $img;
        }

        //发现-返回集合
        if( $area==5 ){
            $list1 = (new \yii\db\Query())
                ->select("topid,toptype, title, imgguid, postcount, url,sguid")
                ->from('post_top')->where('area=4 AND toptype!=2 AND status=1')
                ->offset(0)->limit($limit)
                ->orderBy('topid DESC')
                ->all();
            foreach($list1 AS $key=>$one){
                $img = '';
                $file = \humhub\modules\file\models\File::findOne(['guid' => $one['imgguid']]);
                if( $file ){
                    if ($file->getMimeBaseType() == "image"){
                        //$img = $file->getPreviewImageUrl(200, 200);
                        $img = $file->getUrl();
                    }
                }
                unset($list1[$key]['imgguid']);
                $list1[$key]['image'] = $img;
            }
        }

        //插入直播入口
        $sxb_count = self::getSxb();
        if( $sxb_count ){
            $_sxb = (new \yii\db\Query())
                ->select("topid,toptype, title, imgguid, postcount, url")
                ->from('post_top')->where('area='.$area.' AND toptype=2 AND status=1')
                ->orderBy('topid DESC')
                ->one();
            if($_sxb){
                $img = '';
                $file = \humhub\modules\file\models\File::findOne(['guid' => $_sxb['imgguid']]);
                if( $file ){
                    if ($file->getMimeBaseType() == "image"){
                        //$img = $file->getPreviewImageUrl(200, 200);
                        $img = $file->getUrl();
                    }
                }
                unset($_sxb['imgguid']);
                $_sxb['image'] = $img;

                if( is_array($list2) ){
                    array_unshift($list2, $_sxb);
                }else{
                    $list2[] = $_sxb;
                }
            }
        }

        if( $area==5 ){
            return Array(
                'ad' => $list1,
                'list' => $list2,
            );
        }
        return Array(
            'list' => $list2,
        );
    }

    /**
     * 直播是否存在
     */
    private function getSxb(){
        return 2;
        //获取房间总数
        //返回总数
        return false;
    }
}
